package config;

import cn.entity.Course;
import cn.entity.CourseRepository;
import cn.entity.Student;
import cn.entity.StudentRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = Config.class)

public class test {

    @Autowired
    private WebApplicationContext wac;
    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private CourseRepository courseRepository;

    @Test
//    @Transactional
    public void add() {
//
        Student s1=new Student();
        Student s2=new Student();
        Course c1=new Course();
        Course c2=new Course();
        Course c3=new Course();
        c1.setName("JavaEE");
//       List<Student> students=new ArrayList<Student>();
//       students.add(s1);
//       c1.setStudents(students);
        c2.setName("PHP");
        c3.setName("数学建模");
        s1.setName("张三");
        s2.setName("李四");
        List<Course> courses=new ArrayList<Course>();
        courses.add(c1);
        courses.add(c2);
        courses.add(c3);
        s1.setCourses(courses);
      //  courseRepository.saveAll(courses);
        studentRepository.save(s1);
      //  courseRepository.save(c1);
       // courseRepository.saveAll(courses);
    }
    @Test
    public void delete()
    {
        Student s1=new Student();
        s1.setName("张三");
        s1.setId(3);
        studentRepository.delete(s1);
    }

}
